from socket import *
from time import ctime
import struct
import os

HOST = '10.10.7.2'
PORT = 21345
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
        print('myIP'+HOST+'waiting for connection...')
        tcpCliSock, addr = tcpSerSock.accept()
        print('...connnecting from:', addr)

        filepath = tcpCliSock.recv(BUFSIZ)
    
        print(os.path.basename(filepath.decode()))
        fhead = struct.pack(b'128sq',bytes(os.path.basename(filepath.decode()),encoding='utf-8'), os.stat(filepath).st_size)

        tcpCliSock.send(fhead)
        fp = open(filepath,'rb')
    
        # print(fp)
        a = ''.encode()
        while True:
                data = fp.read(1024)
                # print(data)
                a = a+data
                if not data:
                        break
                tcpCliSock.send(data)
        print("send Over")
        tcpCliSock.close()
         # tcpCliSock.send('[%s] %s' %(bytes(ctime(),'utf-8'),data))
         # tcpCliSock.send(('[%s] %s' % (ctime(), data)).encode())
    
tcpSerSock.close()
